<template>
  <div class="page">
    <el-form :model="searchForm" label-position="left" inline label-suffix="：" size="small">
      <div class="flex wap flex-start">
        <el-form-item label="课程">
          <check-lesson @checkedItem="ScheckedLession" :itemId="searchForm.lessionid" />
        </el-form-item>
        <el-form-item label="类型">
          <check-lesson-type @checkedItem="ScheckedTypeid" :itemId="searchForm.typeid" />
        </el-form-item>
        <el-form-item label="卡">
          <check-card @checkedItem="ScheckedCardid" :itemId="searchForm.cardid" />
        </el-form-item>
        <el-form-item label="">
          <el-button type="primary" @click="getData">查询</el-button>
        </el-form-item>
      </div>
    </el-form>
    <div class="flex wap flex-start" style="padding: 0px 0 10px 0">
      <el-button type="success" @click="addData">新增</el-button>
      <div style="width:20px"></div>
      <!-- <el-button type="danger" @click="del">删除</el-button> -->
    </div>
    <el-table :data="tableData" border style="width: 100%"  height="600"  :header-cell-style="{
        background: '#eef1f6',
        color: '#606266',
        'text-align': 'center'
      }"
      :cell-style="{ 'text-align': 'center' }">
      <!-- <el-table-column type="selection"></el-table-column> -->
      <el-table-column prop="lessionname" label="课程名称"></el-table-column>
      <el-table-column prop="typename" label="类型名称"></el-table-column>
      <el-table-column prop="cardname" label="卡名称"></el-table-column>
      <el-table-column prop="times" label="课次"></el-table-column>
      <el-table-column prop="price" label="价格"></el-table-column>
      <el-table-column prop="createtime" label="创建时间"></el-table-column>
      <el-table-column prop="isdel" label="状态">
        <template slot-scope="scop">
          <el-tag type="success" v-if="scop.row.isdel == 0">暂停</el-tag>
          <el-tag type="warning" v-if="scop.row.isdel == 1">启用</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="" label="操作" width="120">
        <template slot-scope="scop">
          <el-button type="primary" icon="el-icon-edit" circle @click="edit(scop.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle @click="del(scop.row.lessiontypecardid)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="float:right;padding:10px">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10, 20, 50, 100, 200]"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <el-dialog title="学费设置" center :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="editForm" ref="editSetForm" :rules="rule" label-width="100px" class="edit-form" label-suffix="：">
        <el-form-item label="课次数" prop="times">
          <el-input type="text" placeholder="请输入手机号" maxlength="11" v-model="editForm.times"></el-input>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input type="text" placeholder="请输入密码" v-model="editForm.price"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="isdel">
          <el-radio-group v-model="editForm.isdel">
            <el-radio :label="0">禁用</el-radio>
            <el-radio :label="1">启用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="新增学费信息" center :visible.sync="addDialogFormVisible" width="30%">
      <el-form :model="addForm" ref="addSetForm" :rules="addrule" label-width="100px" class="edit-form" label-suffix="：">
        <el-form-item label="课程" prop="lessionid">
          <check-lesson @checkedItem="checkedLession" :itemId="addForm.lessionid" />
        </el-form-item>
        <el-form-item label="类型" prop="typeid">
          <check-lesson-type @checkedItem="checkedTypeid" :itemId="addForm.typeid" />
        </el-form-item>
        <el-form-item label="卡" prop="cardid">
          <check-card @checkedItem="checkedCardid" :itemId="addForm.cardid" />
        </el-form-item>
        <el-form-item label="课次数" prop="times">
          <el-input type="text" placeholder="请输入课次数" v-model="addForm.times"></el-input>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input type="text" placeholder="请输入价格" v-model="addForm.price"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="isdel">
          <el-radio-group v-model="addForm.isdel">
            <el-radio :label="0">禁用</el-radio>
            <el-radio :label="1">启用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSave">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { findLessionTypeCard, updateLessionTypeCard, addLessionTypeCard, delLessionTypeCard } from "@/api/user";
import checkLessonType from "@/views/components/checkLessonType";
import checkLesson from "@/views/components/checkLesson";
import checkCard from "@/views/components/checkCard";
export default {
  components: {
    checkLessonType,
    checkLesson,
    checkCard
  },
  data() {
    return {
      total: 0,
      pageNum: 1,
      pageSize: 10,
      tableData: [],
      searchForm: {
        lessionid:'',
        typeid:'',
        cardid:''
      },
      dialogFormVisible: false,
      addDialogFormVisible: false,
      addForm: {
        lessionid: '',
        typeid: '',
        cardid: '',
        times: '',
        price: '',
        isdel: '',
      },
      editForm: {
        lessiontypecardid: '',
        times: '',
        price: '',
        isdel: '',
      },
      rule: {
        times: [
          { required: true, message: '请设置课次数', trigger: 'change' }
        ],
        price: [
          { required: true, message: '请设置价格', trigger: 'change' }
        ],
        isdel: [
          { required: true, message: '请选择状态', trigger: 'change' }
        ]
      },
      addrule: {
        lessionid: [
          { required: true, message: '请选择课程', trigger: 'change' }
        ],
        typeid: [
          { required: true, message: '请选择类型', trigger: 'change' }
        ],
        cardid: [
          { required: true, message: '请选择卡', trigger: 'change' }
        ],
        times: [
          { required: true, message: '请设置课次数', trigger: 'change' }
        ],
        price: [
          { required: true, message: '请设置价格', trigger: 'change' }
        ],
        isdel: [
          { required: true, message: '请选择状态', trigger: 'change' }
        ]
      },

    };
  },
  computed: {
    queryData() {
      return {
        lessionid: this.searchForm.lessionid,
        typeid: this.searchForm.typeid,
        cardid: this.searchForm.cardid,
        pageNum: this.pageNum,
        pageSize: this.pageSize
      };
    }
  },
  mounted() {
    this.getData();
  },
  methods: {
    async getData() {
      const res = await findLessionTypeCard.post(this.queryData);
      if (res.resultCode === "000000") {
        this.tableData = res.data.lessionTypeCardList ? res.data.lessionTypeCardList : res.data
        this.total = res.data.count ? res.data.count : res.data.length;
      } else {
        this.$message.error(res.resultInfo);
      }
    },
    edit(row) {
      this.dialogFormVisible = true
      const { times, price, isdel, lessiontypecardid } = row
      this.editForm = {
        lessiontypecardid,
        times,
        price,
        isdel,
      }
    },
    save() {
      this.$refs.editSetForm.validate(async (valid) => {
        if (valid) {
          const res = await updateLessionTypeCard.post(this.editForm);
          if (res.resultCode === "000000") {
            this.$message.success(res.resultInfo);
            this.dialogFormVisible = false
            this.getData()
          } else {
            this.$message.error(res.resultInfo);
          }
        } else {
          return false;
        }
      });
    },
    addData() {
      this.addDialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['addSetForm'].resetFields();
      })
      this.addForm = {
        lessionid: '',
        typeid: '',
        cardid: '',
        times: '',
        price: '',
        isdel: '',
      }
    },
    addSave() {
      this.$refs.addSetForm.validate(async (valid) => {
        if (valid) {
          const res = await addLessionTypeCard.post(this.addForm);
          if (res.resultCode === "000000") {
            this.$message.success(res.resultInfo);
            this.addDialogFormVisible = false
            this.getData()
          } else {
            this.$message.error(res.resultInfo);
          }
        } else {
          return false;
        }
      });
    },
    checkedLession(val) {
      this.addForm.lessionid = val
    },
    checkedTypeid(val) {
      this.addForm.typeid = val
    },
    checkedCardid(val) {
      this.addForm.cardid = val
    },
    ScheckedLession(val) {
      this.searchForm.lessionid = val
    },
    ScheckedTypeid(val) {
      this.searchForm.typeid = val
    },
    ScheckedCardid(val) {
      this.searchForm.cardid = val
    },
    del(id) {
      this.$confirm("此操作将删除该项目, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.delItem(id)
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    async delItem(id) {
      const res = await delLessionTypeCard.post({ lessiontypecardid: id });
      if (res.resultCode === "000000") {
        this.$message.success(res.resultInfo);
        this.getData()
      } else {
        this.$message.error(res.resultInfo);
      }
    },
    handleSizeChange(val) {
      this.pageNum = 1;
      this.pageSize = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getData();
    }
  }
};
</script>
<style lang="less" scoped></style>
